package com.tros.customer.features;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UpdateInfo
 */
@WebServlet("/UpdateInfo")
public class UpdateInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("----------> came here for update");		

		 Connection connect =null;			 
		 HttpSession ses = request.getSession();
		 System.out.println("---------cust id-------"+ses.getAttribute("currentID"));
		 
			try{
			       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			       connect =DriverManager.getConnection("jdbc:odbc:bank","root", " ");
		           System.out.println("DB Connection created :"+connect);
		           
		           updateExistUsers(connect,ses,request);
		           updateSavAcc(connect,ses,request);
		           updateChkAcc(connect,ses,request);
		           ses.setAttribute("currentUser", request.getParameter("lname"));
		          
				   response.sendRedirect("custHome.jsp");
			}
			catch(Exception e)
			{ 
		       	e.printStackTrace();
			}
			finally{
				try{
					connect.close();
				}catch(Exception e)
				{
					e.getStackTrace();
				}
			}
	}

	private void updateChkAcc(Connection connect, HttpSession ses,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		try{
        	
	           PreparedStatement state=connect.prepareStatement( "UPDATE CHCKACC SET FIRSTNAME=?,LASTNAME=? WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));	           
	             
	           state.setString(1,request.getParameter("fname"));
	   		   state.setString(2,request.getParameter("lname"));
	   		   
	   	       state.executeUpdate();
	   	       
	   	       state.close();
	  		   
	  		   System.out.println("---------> connect value ------"+connect);
	          
	        	
	        }catch(Exception e){
	        	
	        	e.printStackTrace();        	
	        }
		
	}

	private void updateSavAcc(Connection connect, HttpSession ses,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		try{
        	
	           PreparedStatement state=connect.prepareStatement( "UPDATE SAVACC SET FIRSTNAME=?,LASTNAME=? WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));	           
	             
	           state.setString(1,request.getParameter("fname"));
	   		   state.setString(2,request.getParameter("lname"));
	   		   
	   	       state.executeUpdate();
	   	       
	   	       state.close();
	  		   
	  		   System.out.println("---------> connect value ------"+connect);
	          
	        	
	        }catch(Exception e){
	        	
	        	e.printStackTrace();        	
	        }
		
	}

	private void updateExistUsers(Connection connect, HttpSession ses, HttpServletRequest request) {

		// TODO Auto-generated method stub
        try{
        	
           PreparedStatement state=connect.prepareStatement( "UPDATE EXISTUSERS SET FIRSTNAME=?,LASTNAME=?,ADDRESS=?,DATEOFBIRTH=?,GENDER=?,EMAIL=?,NUMBER=? WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));	           
             
           state.setString(1,request.getParameter("fname"));
   		   state.setString(2,request.getParameter("lname"));
   		   state.setString(3,request.getParameter("address"));
   		   state.setString(4,request.getParameter("dob"));
   		   state.setString(5,request.getParameter("gen"));
   		   state.setString(6,request.getParameter("email"));
   		   state.setString(7,request.getParameter("phoneno"));
   		   
   	       state.executeUpdate();
   	       
   	       state.close();
  		   
  		   System.out.println("---------> connect value ------"+connect);
          
        	
        }catch(Exception e){
        	
        	e.printStackTrace();        	
        }
		
	}

}
